﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Engine.Sgml.Merging;
using Engine.Sgml.Validation.Rules;
using System.Diagnostics;

namespace Engine.Sgml.Allocation {
    internal sealed class InBlockAllocationStrategy : IAllocationStrategy {
        private BlockNodeNavigator blockNodeNavigator;
        private InconsistentBlockAllocator blockAllocator;

        public InBlockAllocationStrategy(ValidationRuleCollection rules) {
            Guard.ArgumentNotNull(rules, "rules");
            this.blockAllocator = new InconsistentBlockAllocator(rules);
            this.blockNodeNavigator = new BlockNodeNavigator();
            }

        public void Allocate(IList<InconsistentBlock> blocks, AllocationContext context) {
            Guard.ArgumentNotNull(blocks, "blocks");
            Guard.ArgumentNotNull(context, "context");
            for (int i = 0; i < blocks.Count; i++) {
                InconsistentBlock block = blocks[i];
                blockNodeNavigator.Reset(block);
                while (blockNodeNavigator.MoveToNext()) {
                    blockAllocator.Allocate(block, blockNodeNavigator.Current);
                    }
                blocks.RemoveAt(i--);
                }
            }
        }
    }
